Counting regex matches

Hi all,

Is it possible to count the regex matches?

I have a string tmpStr = "1234, 2345, 3456, 4567"

I would like to know how many groups of digits there are.

How do I do this in regex please?

Thanks
Neal
Neal_Middlemore - Wed Apr 25 15:24:18 EDT 2012

Re: Counting regex matches
llandale - Wed Apr 25 16:19:36 EDT 2012

Regexp re = regexp2("([0-9][0-9][0-9][0-9])(.*)")
 
void    Count(string InString)
{
        int     Count = 0
        string  Remainder = InString
        while (re Remainder)
        {  Count++
           if (Count < 6) print Remainder "\t[" Remainder[match 1] "]\t[" Remainder[match 2]"]\n"
           Remainder = Remainder[match 2]   // the (.*) in the re
        }
        infoBox("[" InString "]  = " Count "")
}
Count("1234, 2345, 3456, 4567")


-Louie

Re: Counting regex matches
Neal_Middlemore - Wed Apr 25 17:25:45 EDT 2012

llandale - Wed Apr 25 16:19:36 EDT 2012

Regexp re = regexp2("([0-9][0-9][0-9][0-9])(.*)")
 
void    Count(string InString)
{
        int     Count = 0
        string  Remainder = InString
        while (re Remainder)
        {  Count++
           if (Count < 6) print Remainder "\t[" Remainder[match 1] "]\t[" Remainder[match 2]"]\n"
           Remainder = Remainder[match 2]   // the (.*) in the re
        }
        infoBox("[" InString "]  = " Count "")
}
Count("1234, 2345, 3456, 4567")


-Louie

Thanks Louie, spot on!